/*
 * BanBu_SmileView
 * @description 本文件提供对话各种素材的view
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话各种素材的view
 */


#import "BanBu_SmileView.h"
#import "AppCommunicationManager.h"
#import "TKLoadingView.h"
#import "BanBu_ChatViewController.h"

#define DeleteTag 999

@implementation BanBu_SmileView

@synthesize type = _type;
@synthesize delegate = _delegate;
@synthesize inputedStr = _inputedStr;
@synthesize typeLay=_typeLay;
@synthesize gifView=_gifView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImageView *bkView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];//-40
        bkView.image = [UIImage imageNamed:@"chatbg.png"];
        [self addSubview:bkView];
        [bkView release];
        
        UIScrollView *scrool = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8, 320, 185)];
        scrool.delegate = self;
        scrool.backgroundColor = [UIColor clearColor];
        scrool.contentSize = CGSizeMake(320*1, 180);
        scrool.showsHorizontalScrollIndicator = NO;
        scrool.pagingEnabled = YES;
        
        _contengView = scrool;
        _contengView.tag=1000;

        UIScrollView *scrool2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8, 320, 185)];
        scrool2.delegate = self;
        scrool2.backgroundColor = [UIColor clearColor];
        scrool2.contentSize = CGSizeMake(320*1, 180);
        scrool2.showsHorizontalScrollIndicator = NO;
        scrool2.pagingEnabled = YES;
        
        self.contengViewBackup1 = scrool2;
        self.contengViewBackup1.tag=1001;
        
        UIScrollView *scrool3 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8,320 , 185)];//410
        scrool3.delegate = self;
        scrool3.backgroundColor = [UIColor clearColor];
        
        self.contengViewBackup2 = scrool3;
        self.contengViewBackup2.tag=1002;
        
        [self addSubview:scrool];
        [scrool release];
        
        [self addSubview:scrool2];
        [self addSubview:scrool3];
        [scrool2 release];
        [scrool3 release];
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-40, __MainScreen_Width, 40)];
        imageView.image = [UIImage imageNamed:@"bottom-bar.png"];
        [self addSubview:imageView];
        [imageView release];
        
        UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 164, 320, 10)];
        pageControl.backgroundColor = [UIColor clearColor];
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
        pageControl.currentPage = 0;
        _pageControl = pageControl;
        [self addSubview:pageControl];
        [pageControl release];
        
        _type=6;
        

    }
    return self;
}

-(void)setTypeLay:(SmileViewLayoutType)typeLay
{
   if(_typeLay==typeLay)
    {
        return;
    }
    _typeLay=typeLay;    
}

-(void)layoutSubviews
{
    if(_type!=SmileViewAddType)
    {
        _inputedStr = [[NSMutableArray alloc] initWithCapacity:10];
    }
    else if(_type == SmileViewAddType)
    {
        for(UIView *view in self.subviews)
        {
            switch (view.tag)
            {
                case 10000:
                case 10001:
                case 10002:
                case DeleteTag:
                    view.hidden = YES;
                    break;
                    
                default:
                    break;
            }
        }
     }
}

#pragma UIScrollView delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_type == SmileViewAddType)
    {
        _pageControl.currentPage = scrollView.contentOffset.x/60;
    }
    else
    {
        _pageControl.currentPage = scrollView.contentOffset.x/320.0;
    }
    NSLog(@"%d",_pageControl.currentPage);
}

- (void)typeChange:(UIButton *)button
{
        [UIView animateWithDuration:0.3
                     animations:^{                         
                         _arrow.frame = CGRectMake(57+128*(button.tag - 10000), self.bounds.size.height-40, 10, 5);
                     }
     completion:^(BOOL finished) {
         _pageControl.currentPage = (button.tag - 10000);
         _contengView.contentOffset = CGPointMake(0, 0);
         self.type = button.tag - 10000;
     }];
}

- (void)setType:(SmileViewType)type
{
    if(_type == type)
        return;
    _type = type;
    
    [self layoutSubviews];
    _pageControl.frame = CGRectMake(0, 164, 320, 10);

    {
        self.contengViewBackup1.hidden = YES;
        self.contengViewBackup2.hidden = NO;
        _contengView.hidden = YES;
        _pageControl.numberOfPages = 2;
        _pageControl.frame = CGRectMake(0, 190, 320, 10);
        if (!self.s3)
        {
            self.contengViewBackup2.contentSize= CGSizeMake(320*1+90+110, 140);

            NSMutableArray *nameArr=[[NSMutableArray alloc]initWithObjects:@"拍照",@"相册",@"涂鸦",@"名片",@"破冰语",@"manuscript",@"位置",@"魔法工具箱", @"burnAfterReading",@"magicWords",nil];
            
            
            NSMutableArray *labelArr=[[NSMutableArray alloc]initWithObjects:NSLocalizedString(@"photo2Video", nil),NSLocalizedString(@"funLabel1", nil),NSLocalizedString(@"funLabel3",nil),NSLocalizedString(@"visitingCard", nil),NSLocalizedString(@"searchIce", nil),NSLocalizedString(@"collectionButton", nil),NSLocalizedString(@"locationBtn", nil),NSLocalizedString(@"magicTool", nil),NSLocalizedString(@"burnAfterReading", nil),NSLocalizedString(@"magicWords",nil),nil];
            
            if(_delegate&&[_delegate respondsToSelector:@selector(addActionCard)])
            {
                NSString *name=[_delegate addActionCard];
                [nameArr addObject:name];
            }
        
            NSInteger page=0;
            for(int i=0;i<[nameArr count];i++)
            {
                NSString *imageName = [NSString stringWithFormat:@"%@.png",[nameArr objectAtIndex:i]];
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(320*page+32+100*(i%5), 87*(i/5-2*page), 55, 55);
                [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
                [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(actionToadd:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = i;
                [self.contengViewBackup2 addSubview:button];
                
                UILabel *label=[[UILabel alloc]init];
                label.frame=CGRectMake(320*page+15+100*(i%5)-10, 87*(i/5-2*page)+60, 85+20, 15);
                label.textAlignment=kTextAlignmentCenter;
                label.font = [UIFont systemFontOfSize:14];
                label.backgroundColor=[UIColor clearColor];
                NSString *str= [labelArr objectAtIndex:i];
                label.text=str;
                [self.contengViewBackup2 addSubview:label];
                [label release];
                
                if(i>8 && !page)
                    page = 1;
            }
            [nameArr release];
            [labelArr release];
            
            self.s3 = YES;
        }
    }
}

-(void)addExpressionIcon:(UIButton *)butten
{
    NSArray *typeArr = [NSArray arrayWithObjects:@"text",@"image",@"sound", nil];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    [parDic setValue:[typeArr objectAtIndex:1] forKey:@"kind"];
    [parDic setValue:@"ＱＱ表情" forKey:@"keyword"];
    [parDic setValue:[MyAppDataManager getLanguageSubmitToServer] forKey:@"lang"];
    [parDic setValue:[MyAppDataManager getLanguageSubmitToServer] forKey:@"lang"];
    [AppComManager getBanBuData:BanBu_Get_Sayhi_Rand par:parDic delegate:_chatViewController];
    [TKLoadingView showTkloadingAddedTo:_chatViewController.view title:NSLocalizedString(@"loadingNotice", nil) activityAnimated:YES];
}

-(void)btnPress:(UITapGestureRecognizer *)tap
{
    NSString *inputStr=nil;
    
    NSArray  *smileArr = [NSArray arrayWithObjects:@"打怪兽",@"嘟嘟嘴",@"多变",@"饿了",@"高兴",@"歌王",@"好烦啊",@"加油",@"惊讶",@"可爱",@"可怜",@"麦霸",@"怒火",@"饶命",@"撒娇",@"睡觉",@"痛苦",@"羞羞脸",@"阴谋",@"眨眼",@"发大财",@"喷饭",@"炸飞了",@"凄凉",@"T台秀",@"读书",@"扭屁股",@"舞蹈",@"得意",@"运动",@"救命",@"滚远点",@"江南舞",@"走你", nil];
    inputStr=[smileArr objectAtIndex:tap.view.tag];
        
    if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
        [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:1];
}

- (void)btnPressed:(UIButton *)button
{
    if(button.tag == DeleteTag)
    {
        if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
            [_delegate banBu_SmileView:self didInputSmile:nil isDelete:YES type:0];
    }
    else 
    {
        NSString *inputStr = nil;
        if(_type == SmileViewCharactersPaintedType)
        {    inputStr = button.titleLabel.text;
            [_inputedStr addObject:inputStr];
            if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
                [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:0];
        }
        else
        {
            NSString *smileStr = @"[吃包子]|[打怪兽]|[大跌眼镜]|[高歌一曲]|[歌舞王后]|[加油]|[泪流满面]|[路过]|[怒发冲冠]|[俏皮]|[求包养]|[求饶]|[睡觉]|[太帅了]|[头都大了]|[兴高采烈]|[羞羞脸]|[压力好大]|[阴谋得逞]|[睁大眼睛]|[装可爱]";
            inputStr = [NSString stringWithFormat:@"%@",[[smileStr componentsSeparatedByString:@"|"] objectAtIndex:button.tag]];
            [_inputedStr addObject:inputStr];
            if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
                [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:1];
        }
    }    
}

-(void)actionToadd:(UIButton *)bt
{
    if(_delegate&&[_delegate respondsToSelector:@selector(banBu_ActionView:didInputBrand:isAdd:)])
    {
         if(bt.tag==0)
         {
             [_delegate banBu_ActionView:self didInputBrand:bt isAdd:YES];
         }
         else
         {
             [_delegate banBu_ActionView:self didInputBrand:bt isAdd:NO];
         }
    }
}

- (void)dealloc
{
    self.delegate = nil;
    self.inputedStr = nil;
    
    [_contengViewBackup1 release],_contengViewBackup1 = nil;
    [_contengViewBackup2 release],_contengViewBackup2 = nil;
    
    [super dealloc];
}

@end
